﻿using STHPMS1.DAL.D_Core;
using STHPMS1.DAL.D_Core;
using STHPMS1.ICore.I_DAL.Repository;
using STHPMS1.ICore.I_Entity.Stmpms1;
using System.Collections.Generic;
using System.Data;
using ServiceStack.OrmLite;
using System.Text;
using System.Data.SqlClient;
using System;
using System.Linq.Expressions;
using ServiceStack.OrmLite.SqlServer;

namespace STHPMS1.DAL.D_Repository
{
    public class B_HistoryRepository : DalBase, IB_HistoryRepository
    {
        /// <summary>
        /// 拆分一个新的路面结构
        /// </summary>
        /// <param name="oldStruid"></param>
        /// <param name="p"></param>
        public void AddNewData(string oldStruid, IB_Pmstc p)
        {
            //TODO:路面结构厚度分为层总厚度，面层结构厚度
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("insert into B_PMSTC values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}')",p.Struid,p.Rdwayid,p.Beginmp,p.Endmp,p.Direction_Dictid,p.Pvmttype_Dictid,p.Basictype_Dictid,p.Cbr,p.Degdf,p.Thick_T,p.Importdate,p.His_Version,p.Thick_S);
            sql.AppendFormat(@"insert into B_LAYER 
select '{1}'+'_'+ CONVERT(varchar(3), LAYER_DICTID),RDWAYID,'{1}',LAYER_DICTID,STRU_ID,
THICK,POIRAT,ELAMOD,LAYERCOEFF,DRAINCOEFF,IMPORTDATE ,DIRECTION_DICTID,HIS_VERSION
from B_LAYER where STRUID='{0}'",oldStruid,p.Struid);
            using (IDbConnection conn = Adapter.OpenDbConnection())
            {
                IDbTransaction tran = conn.BeginTransaction();
                try
                {
                    IDbCommand cmmd = conn.CreateCommand();
                    cmmd.Transaction = tran;
                    cmmd.CommandText = sql.ToString();
                    cmmd.ExecuteNonQuery();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }
        }
        public void MoveOldData(string oldStruid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("insert into B_PMSTC_HISTORY select STRUID,HIS_VERSION,RDWAYID,BEGINMP,ENDMP,DIRECTION_DICTID,PVMTTYPE_DICTID,BASICTYPE_DICTID,CBR,DEGDF,THICK,IMPORTDATE from B_PMSTC where STRUID='{0}';", oldStruid);
            sql.AppendFormat("insert into B_LAYER_HISTORY select LAYID,HIS_VERSION,RDWAYID,STRUID,LAYER_DICTID,DIRECTION_DICTID,STRU_ID,THICK,POIRAT,ELAMOD,LAYERCOEFF,DRAINCOEFF,IMPORTDATE from B_LAYER where STRUID='{0}';", oldStruid);
            sql.AppendFormat("delete from B_PMSTC where STRUID='{0}';", oldStruid);
            sql.AppendFormat("delete from B_LAYER where STRUID='{0}';", oldStruid);
            using (IDbConnection conn = Adapter.OpenDbConnection())
            {
                IDbTransaction tran = conn.BeginTransaction();
                try
                {
                    IDbCommand cmmd = conn.CreateCommand();
                    cmmd.Transaction = tran;
                    cmmd.CommandText = sql.ToString();
                    cmmd.ExecuteNonQuery();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }
        }
        public void UpdateHistory(string oldStruid,string newStruid,double mp)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("update B_PMSTC set STRUID='{0}',beginmp='{1}' where STRUID='{2}';", newStruid, mp, oldStruid);
            sql.AppendFormat("update B_LAYER set STRU_ID='{0}' where STRU_ID='{1}'",newStruid,oldStruid);
            using (IDbConnection conn = Adapter.OpenDbConnection())
            {
                IDbTransaction tran = conn.BeginTransaction();
                try
                {
                    IDbCommand cmmd = conn.CreateCommand();
                    cmmd.Transaction = tran;
                    cmmd.CommandText = sql.ToString();
                    cmmd.ExecuteNonQuery();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }
        }

        public void UpdateXTHistroy(IB_Pmstc p, IB_Layer[] lstLayer)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("update B_PMSTC set HIS_VERSION='{0}',THICK_T='{1}',THICK_S='{2}' where STRUID='{3}'",p.His_Version,p.Thick_T,p.Thick_S,p.Struid);
            foreach(IB_Layer l in lstLayer)
            {
                sql.AppendFormat("update B_LAYER set STRU_ID='{0}',THICK='{1}',HIS_VERSION='{2}' where LAYID='{3}'",l.Stru_Id,l.Thick,l.His_Version,l.Layid);
            }
            using (IDbConnection conn = Adapter.OpenDbConnection())
            {
                IDbTransaction tran = conn.BeginTransaction();
                try
                {
                    IDbCommand cmmd = conn.CreateCommand();
                    cmmd.Transaction = tran;
                    cmmd.CommandText = sql.ToString();
                    cmmd.ExecuteNonQuery();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }
        }


        public void UpdateHistory(string oldStruid, IB_Pmstc[] lstPms, IB_Layer[] lstLayer)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("delete from B_PMSTC where STRUID='{0}';", oldStruid);
            sql.AppendFormat("delete from B_LAYER where STRUID='{0}';", oldStruid);
            foreach(IB_Pmstc p in lstPms)
            {
                sql.AppendFormat("insert into B_PMSTC values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}');", p.Struid, p.Rdwayid, p.Beginmp, p.Endmp, p.Direction_Dictid, p.Pvmttype_Dictid, p.Basictype_Dictid, p.Cbr, p.Degdf, p.Thick_T, p.Importdate, p.His_Version, p.Thick_S);
            }
            foreach(IB_Layer l in lstLayer)
            {
                sql.AppendFormat("insert into B_LAYER values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}');",l.Layid,l.Rdwayid,l.Struid,l.Layer_Dictid,l.Stru_Id,l.Thick,l.Poirat,l.Elamod,l.Layercoeff,l.Draincoeff,l.Importdate,l.His_Version,l.Direction_Dictid);
            }
            using (IDbConnection conn = Adapter.OpenDbConnection())
            {
                IDbTransaction tran = conn.BeginTransaction();
                try
                {
                    IDbCommand cmmd = conn.CreateCommand();
                    cmmd.Transaction = tran;
                    cmmd.CommandText = sql.ToString();
                    cmmd.ExecuteNonQuery();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }
        }

        public void AddHistory(IB_History_Details h_ds, IB_History_Layer_Details[] h_l_ds)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("insert into B_History_Details values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}');", h_ds.Detailsid,h_ds.Rdwayid,h_ds.Direction_Dictid,h_ds.Beginmp,h_ds.Endmp,h_ds.Year,h_ds.His_Version,h_ds.Pave_Type);
            foreach(IB_History_Layer_Details h_l_d in h_l_ds)
            {
                sql.AppendFormat("insert into B_History_layer_Details values('{0}','{1}','{2}','{3}','{4}','{5}');", h_l_d.Detailsid,h_l_d.Layer_Dictid, h_l_d.MillingThick, h_l_d.OverLayThick, h_l_d.Stru_Id, h_l_d.History_LayerId);
            }
            using (IDbConnection conn = Adapter.OpenDbConnection())
            {
                IDbTransaction tran = conn.BeginTransaction();
                try
                {
                    IDbCommand cmmd = conn.CreateCommand();
                    cmmd.Transaction = tran;
                    cmmd.CommandText = sql.ToString();
                    cmmd.ExecuteNonQuery();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }
        }
    }
}
